﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OOAD_Project_Engine.Foundation;
using Microsoft.Xna.Framework;
using OOAD_Project_Engine.Foundation.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using OOAD_Project_Engine.Domain.Components;
using OOAD_Project_Engine.Foundation.MyEventArgs;

namespace OOAD_Project_Engine.Domain.Entities
{
    [Serializable()]
    public class Entity : EntityComponentCollection, IEntity
    {
        IEntity parent;

        public string Name
        {
            get;
            private set;
        }

        public Guid ID
        {
            get;
            private set;
        }

        public Entity(string name)
            : this(name, new IEntityComponent[] { }) { }

        public Entity(string name, params IEntityComponent[] components)
        {
            this.Name = name;

            if (components != null && components.Length > 0)
            {
                foreach (IEntityComponent component in components)
                {
                    Add(component);
                }
            }

            this.ID = Guid.NewGuid();
        }

        protected override void OnComponentAdded(Foundation.MyEventArgs.EntityComponentEventArgs e)
        {
            EntityComponent ec = e.Component as EntityComponent;
            ec.Owner = this;
            base.OnComponentAdded(e);
        }

        public IEntity Parent
        {
            get
            {
                return parent;
            }
            set
            {
                if (!IsReadOnly)
                {
                    if (value != this && parent != value)
                    {
                        parent = value;

                        OnParentChanged(new EntityEventArgs(value));
                    }
                }
            }
        }

        public event EventHandler<EntityEventArgs> ParentChanged;

        private void OnParentChanged(EntityEventArgs e)
        {
            if (ParentChanged != null)
            {
                ParentChanged(this, e);
            }
        }
        EnabledState recordedState;
        public bool Enabled
        {
            get
            {
                foreach (IEntityComponent component in FindAll<IEntityComponent>())
                {
                    if (component.Enabled)
                        return true;
                }
                return false;
            }
            set
            {
                List<IEntityComponent> components = FindAll<IEntityComponent>();

                if (value)
                {
                    if (recordedState != null)
                    {
                        foreach (IEntityComponent component in components)
                        {
                            EntityComponent ec = component as EntityComponent;
                            ec.Enabled = recordedState[component];
                        }
                    }
                    recordedState = null;
                }
                else
                {
                    recordedState = new EnabledState(this);

                    foreach (IEntityComponent component in components)
                    {
                        EntityComponent ec = component as EntityComponent;
                        ec.Enabled = false;
                    }
                }

            }
        }

        public IComponentManager Manager
        {
            get;
            set;
        }

    }
}
